package cate.game.role.friend;

import cate.common.table.d.GDFriend;
import cate.common.util.GameResult;
import cate.game.role.Role;
import cate.game.role.RoleDependent;
import cate.game.role.friend.msg.FriendAddBlackListResp;
import cate.game.role.friend.msg.FriendRemoveBlackListResp;
import com.fasterxml.jackson.annotation.JsonIgnore;
import easy.java.dev.note.NoteField;

import java.util.HashMap;
import java.util.Map;

public class BlackList extends RoleDependent {

    @NoteField(value = "黑名单")
    public Map<String, Friend> items;

    @Override
    public void initialize(Role role) {
        super.initialize(role);
        if (items == null) {
            items = new HashMap<>();
        }
    }

    public GameResult<Void> addCheck(Role black){
        GameResult<Void> r = new GameResult<>();
        if(isFull()){
            return r.fail("黑名单已达上限， 请先清理");
        }
        if(existed(black.getUid())){
            return r.fail("该玩家已经在黑名单中");
        }
        return r;
    }

    public void add(Role black){
        Friend f = new Friend(black);
        items.put(black.getUid(), f);
        role.sendNow(new FriendAddBlackListResp(f));
    }

    public void remove(String uid){
        if(!existed(uid)){
            role.getGame().notice.message(role, "该玩家不在黑名单中");
            return;
        }
        items.remove(uid);
        role.getGame().notice.message(role,"移除黑名单成功");
        role.sendNow(new FriendRemoveBlackListResp(uid));
    }

    public boolean existed(String uid){
        return items.get(uid) != null;
    }

    @JsonIgnore
    public boolean isFull(){
        return items.size() >= GDFriend.BLACKLIST_MAX;
    }

    @JsonIgnore
    public Map<String, Friend> getBlackList(){
        return items;
    }
}
